﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;
using Planer.UI.Controls;
using Planer.UI;
using Planer.UI.Forms;

namespace Planer
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            CustomInitializeComponent();
        }

        List<billing> billingList;
        List<financial_goal> financicalGoalList;

        private void CustomInitializeComponent()
        {
            userPanel.CurrentUser = Program.CurrentUser;            
            UpdateBillingControls();
            UpdateFinancicalGoalControls();

            billControl = new BillControl();
            billControl.Dock = DockStyle.Fill;
            billControl.Visible = false;
            panelBig.Controls.Add(billControl);

            accumulation accum = AccumulationModel.GetInstance().GetAccumulationByUser(Program.CurrentUser);
            accumulationControl.Accumulation = accum;
        }
        public void UpdateFinancicalGoalControls()
        {
            foreach (Control cnt in flowLayoutPanel1.Controls)
            {
                if (cnt is FinancicalGoalPresentButton)
                    flowLayoutPanel1.Controls.Remove(cnt);
            }
            financicalGoalList = FinancicalGoalModel.GetInstance().GetFinancicalGoalListByUser(Program.CurrentUser);

            foreach (financial_goal goal in financicalGoalList)
            {
                FinancicalGoalPresentButton but = new FinancicalGoalPresentButton();
                but.Goal = goal;
                flowLayoutPanel1.Controls.Add(but);
            }
        }

        public void UpdateBillingControls()
        {
            billingList = BillingModel.GetInstance().GetBillingListByUser(Program.CurrentUser);

            Point startPoint = new Point(0, 0);

            byte btnCount = 0;
            for (; btnCount < billingList.Count && btnCount < panelBillings.Controls.Count; btnCount++ )
            {
                BillPresentButton btn = (BillPresentButton)panelBillings.Controls[btnCount];
                btn.Bill = billingList[btnCount];                
            }

            if (btnCount > billingList.Count)
            {
                for (int i = panelBillings.Controls.Count - 1; i > btnCount; i--)
                {
                    panelBillings.Controls.RemoveAt(i);
                }
            } else
                for (int i = btnCount; i < billingList.Count; i++)
                {
                    billing b = billingList[i];
                    BillPresentButton button = new BillPresentButton();
                    //button.Width = panelBig.Width;
                    //button.Location = new Point(startPoint.X, startPoint.Y + i * button.Height);
                    button.Click += new EventHandler(buttonBillPresent_Click);

                    //MenuItem[] commands = new MenuItem[2];
                    //commands[0] = new MenuItem("Удалить", deleteMenuItem_Click);
                    //commands[0].Tag = button;
                    //commands[1] = new MenuItem("Перевод средств", transferMenuItem_Click);
                    //commands[1].Tag = button;
                    //button.ContextMenu = new ContextMenu(commands);
                    //button.ContextMenu.Tag = button;

                    button.Bill = b;
                    panelBillings.Controls.Add(button);
                }
        }

        BillControl billControl = null;

        private void buttonBillPresent_Click(object sender, EventArgs e)
        {
            foreach (Control cntrl in panelBig.Controls)
            {
                cntrl.Visible = false;
            }
            billControl.Bill = ((BillPresentButton)sender).Bill;
            billControl.Visible = true;
            
            //DateTime st = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            //period p = new period() { is_opened = "y", start_date = st, finish_date = st.AddMonths(1) };
            ////action_category usr = new action_category() { name = "Перевод средств" };
            //Program.Db.AddToperiods(p);
            //Program.Db.SaveChanges();
            
        }


        private void deleteMenuItem_Click(object sender, EventArgs e)
        {
            MenuItem btn = sender as MenuItem;
            if (btn == null) return;
            
            BillPresentButton button = btn.Tag as BillPresentButton;
            if (button == null) return;

            billing bill = button.Bill;
            if (bill.balance > 0)           //перевод средств
            {
            }
            else if (bill.balance == 0)     //удаление
            {

            }
            else
            {

            }

        }
        private void transferMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void buttonAddBill_Click(object sender, EventArgs e)
        {
            string nameInput = Microsoft.VisualBasic.Interaction.InputBox("Введите название нового счета", "Новый счет", "Новый счет");
            if (nameInput == "") return;


            billing bill = new billing() { name = nameInput, balance = 0, create_date = DateTime.Now, user = Program.CurrentUser };

            ModelResult mr = BillingModel.GetInstance().SaveOrUpdateBilling(bill);
            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }           
            UpdateBillingControls();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddFinancicalGoalFrom frm = new AddFinancicalGoalFrom();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                UpdateFinancicalGoalControls();
            }
        }

        public void RefreshBillControl()
        {
            if (billControl.Visible)
            {
                billControl.RefreshControl();
            }
        }
    }
}
